home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / y2l.lha / y2l / yacc.rex < prev   
OS/2 REXX Batch file  |  1992-08-20  |  7KB  |  245 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /* scanner for yacc input language                    */
  4. /*                                    */
  5. /************************************************************************/
  6.  
  7. EXPORT {
  8. # include "StringMem.h"
  9. # include "Positions.h"
  10. # include "Tables.h"
  11.  
  12. typedef struct    { tPosition Position;
  13.              tStringRef        string;
  14.              int        number;
  15.              struct tCell *    action;
  16.         } tScanAttribute;
  17.  
  18. extern void ErrorAttribute ();
  19. }
  20.  
  21. GLOBAL {
  22.  
  23. # include <string.h>
  24.  
  25. # define IDENT        1
  26. # define C_IDENT    2
  27. # define NUMBER        3
  28. # define LEFT        4
  29. # define RIGHT        5
  30. # define NONASSOC    6
  31. # define TOKEN        7
  32. # define PREC        8
  33. # define TYPE        9
  34. # define START        10
  35. # define UNION        11
  36. # define MARK        12
  37. # define LESS        13
  38. # define GREATER    14
  39. # define COMMA        15
  40. # define BAR        16
  41. # define LBRACE        17
  42. # define RBRACE        18
  43. # define SEMICOLON    19
  44. # define COLON        20
  45.  
  46. char UNION1  [] = "\ntypedef union {";
  47. char UNION2  [] = "} YYSTYPE;\n";
  48.  
  49. void ErrorAttribute (Token, Attribute)
  50.    int Token;
  51.    tScanAttribute *Attribute;
  52. {
  53.   Attribute->string = 0;
  54. }
  55.  
  56. int level = 0;    /* nesting level of braces in actions */
  57.  
  58. void get_put ()    /* this procedure has a side-effect */
  59. {
  60.   int l,i;
  61.   char v [256];
  62.  
  63.   l = GetWord (v);
  64.   if (v [0] == '\'')
  65.     for (i = 1; v [i+1] != '\0'; i++)
  66.       if ((v [i-1] != '\\') && (v [i] == '\\') && (v [i+1] == '\''))
  67.     v [i] = '\'';
  68.   Attribute.string = PutString (v, l);
  69.   put_name (Attribute.string);
  70. }
  71.  
  72. char v [256], w [256];
  73. int i, l = 0;
  74. tStringRef r;
  75. }
  76.  
  77. DEFAULT {
  78.    (void) GetWord (w);
  79.    (void) fprintf (stderr, "%3d, %2d: illegal character: '%s'\n",
  80.       Attribute.Position.Line, Attribute.Position.Column, w);
  81. }
  82.  
  83. DEFINE
  84.     Letter    = {A-Za-z._} .
  85.     Digit    = {0-9} .
  86.     Octal    = {0-7} .
  87.     comment    = "/*" -{*}* "*"+ (-{*/} -{*}* "*"+) * "/" .
  88.     ToSkip    = ( {\ \t\n} | comment ) * .
  89.  
  90. START Code, Union, RulePart, Action, String1, String2, Comment, SubPart
  91.  
  92. RULES
  93.  
  94. /* scan idents, literals and numbers */
  95. #RulePart# Letter (Letter | Digit) * / ToSkip ":" :
  96.                   { get_put (); return C_IDENT; }
  97. #STD, RulePart# Letter (Letter | Digit) * :
  98.                   { get_put (); return IDENT; }
  99. #STD, RulePart# ' - {'\\\n} + '    : { get_put ();
  100.                     if (yyStartState == RulePart)
  101.                       put_token (Attribute.string, 0); 
  102.                     return IDENT; }
  103. #STD, RulePart# ' \\ Octal + '    : { get_put ();
  104.                     if (yyStartState == RulePart)
  105.                       put_token (Attribute.string, 0); 
  106.                     return IDENT; }
  107. #STD, RulePart# ' \\ ANY + '    : { get_put ();
  108.                     if (yyStartState == RulePart)
  109.                       put_token (Attribute.string, 0); 
  110.                     return IDENT; }
  111. #STD, RulePart# Digit +        : { (void) GetWord (v);
  112.                     Attribute.number = atoi (v);
  113.                     return NUMBER; }
  114.  
  115. #STD# "%left"        : { return LEFT        ; }
  116. #STD# "%right"        : { return RIGHT    ; }
  117. #STD# "%nonassoc"    : { return NONASSOC    ; }
  118. #STD# "%token"        : { return TOKEN    ; }
  119. #STD, RulePart# "%prec"    : { return PREC        ; }
  120. #STD# "%type"        : { return TYPE        ; }
  121. #STD# "%start"        : { return START    ; }
  122.  
  123. #STD# "%union"        : { yyStart (Union); level = 0; l = 0;
  124.                 put_global (PutString (UNION1, strlen (UNION1))); }
  125. #Union# \{        : { if (level) l += GetWord (&v [l]); level ++; }
  126. #Union# - {{\}\n} *    : { l += GetWord (&v [l]); }
  127. #Union# \n        : { yyEol (0); l += GetWord (&v [l]);
  128.                 put_global (PutString (v, l)); l = 0; }
  129. #Union# \} ;?        : { if (--level) 
  130.                   l += GetWord (&v [l]);
  131.                 else {
  132.                   put_global (PutString (v, l)); l = 0;
  133.                   put_global (PutString (UNION2, strlen (UNION2)));
  134.                   yyPrevious;
  135.                   return UNION;
  136.                 } }
  137.  
  138. #STD#       %%        : { yyStart (RulePart); l = 0; return MARK; }
  139. #RulePart# %%        : { yyStart (SubPart ); l = 0; return MARK; }
  140.  
  141. #STD# "%{"        : { yyStart (Code); l = 0; }
  142. #Code# - {%\n} + | %    : { l += GetWord (&v [l]); }
  143. #Code# \n        : { yyEol (0); l += GetWord (&v [l]);
  144.                 put_global (PutString (v, l)); l = 0; }
  145. #Code# "%}"        : { yyPrevious; v [l++] = '\n';
  146.                 put_global (PutString (v, l)); l = 0; }
  147.  
  148. #STD, RulePart# "<"    : { return LESS        ; }
  149. #STD, RulePart# ">"    : { return GREATER    ; }
  150. #STD, RulePart# ","    : { return COMMA    ; }
  151. #STD, RulePart# "|"    : { return BAR        ; }
  152. #STD, RulePart# ";"    : { return SEMICOLON    ; }
  153. #STD, RulePart# ":"    : { return COLON    ; }
  154.  
  155. /* scanning actions */
  156. #RulePart# "{"        : { yyStart (Action);
  157.                 level = 1;
  158.                 action_list = 0;
  159.                 l = 0; v [0] = '\0';
  160.                 return LBRACE; }
  161. #Action# "{"        : { level++; v [l++] = '{'; }
  162.  
  163. #Action# $ \< - {>} * \> (\- ? Digit + | $) :
  164.               { (void) GetWord (w);
  165.                 v [l++] = '$';
  166.                 for (i = 2; w [i] != '>'; i++);
  167.                 i++;
  168.                 while (v [l++] = w [i++]);
  169.                 l--;
  170.                 (void) strcpy (&v [l], ".");
  171.                 l += 1;
  172.                 for (i = 2; w [i] != '>'; v [l++] = w [i++]); }
  173.  
  174. #Action# $ (\- ? Digit + | $) :
  175.               { i = GetWord (w);
  176.                 (void) strcpy (&v [l], w);
  177.                 l += i;
  178.                 if (w [1] == '$') {
  179.                   r = get_type (0);
  180.                 } else {
  181.                   i = atoi (&w [1]);
  182.                   if (i > 0) r = get_type (i);
  183.                   else       r = 0;
  184.                 }
  185.                 if (r) {
  186.                   StGetString (r, (tString) w);
  187.                   v [l++] = '.';
  188.                   (void) strcpy (&v [l], w);
  189.                   l += strlen (w);
  190.                 } }
  191.  
  192. #Action# '        : { yyStart (String1); v [l++] = '\''; }
  193. #String1# - {'} +    : { l += GetWord (&v [l]); }
  194. #String1# '        : { yyPrevious; l += GetWord (&v [l]); }
  195.  
  196. #Action# \"        : { yyStart (String2); v [l++] = '"'; }
  197. #String2# - {"} +    : { l += GetWord (&v [l]); }
  198. #String2# \"        : { yyPrevious; l += GetWord (&v [l]); }
  199.  
  200. #STD#    "/*"        : { yyStart (Comment); l = GetWord (v); }
  201. #Action# "/*"        : { yyStart (Comment); l += GetWord (&v [l]); }
  202. #Comment# - {\n*} + | "*"    : { l += GetWord (&v [l]); }
  203. #Comment# \n        : { yyEol (0); l+= GetWord (&v [l]);
  204.                 if (yyPreviousStart == Action) {
  205.                   put_action  (PutString (v, l)); l = 0;
  206.                 } else if (yyPreviousStart == STD) {
  207.                   put_global  (PutString (v, l)); l = 0;
  208.                 } else {
  209.                   append_text (PutString (v, l)); l = 0;
  210.                 } }
  211. #Comment# "*/"        : { yyPrevious; l += GetWord (&v [l]);
  212.                 if (yyStartState == STD) {
  213.                   v [l++] = '\n'; put_global (PutString (v, l)); l = 0;
  214.                 } }
  215.  
  216. #String1, String2, Comment# ANY : { v [l++] = '\\'; l += GetWord (&v [l]); }
  217.  
  218. #Action# - {${\}'"/\n} + | {/$}    : { l += GetWord (&v [l]); }
  219. #Action# \n        : { yyEol (0); l += GetWord (&v [l]);
  220.                 put_action (PutString (v, l)); l = 0; }
  221. #Action# "}"        : { if (--level)
  222.                   v [l++] = '}';
  223.                 else {
  224.                   yyStart (RulePart);
  225.                   l += GetWord (&v [l]);
  226.                   l += GetWord (&v [l]);
  227.                   put_action (PutString (v, l)); l = 0;
  228.                   Attribute.action = action_list;
  229.                   return RBRACE;
  230.                 } }
  231.  
  232. /* scan white space and comments between rules */
  233. #RulePart# "/*"        : { yyStart (Comment); l += GetWord (&v [l]); }
  234. #RulePart# " " +    : { l += GetWord (&v [l]); }
  235. #RulePart# \t        : { yyTab; v [l++] = '\t'; }
  236. #RulePart# \n        : { yyEol (0); l += GetWord (&v [l]);
  237.                 append_text (PutString (v, l)); l = 0; }
  238.  
  239. /* scan tail after second %% */
  240. #SubPart# ANY +        : { l += GetWord (&v [l]); }
  241. #SubPart# \n        : { yyEol (0); l += GetWord (&v [l]);
  242.                 put_global (PutString (v, l)); l = 0; }
  243.  
  244. \f | \r            :- {}
  245.